package cn.swing.ushare.passport.initException;

import cn.swing.ushare.common.web.JsonResult;
import cn.swing.ushare.common.web.State;
import com.alibaba.fastjson.JSON;
import org.springframework.http.MediaType;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


/**
 * 登录密码或用户名错误
 */
public class SimpleAuthenticationEntryPoint implements AuthenticationEntryPoint {
    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException e) throws IOException, ServletException {
        response.setContentType("application/json;charset=utf-8");
        response.getWriter().write(JSON.toJSONString(JsonResult.fail(State.ERR_PASSWORD_OR_USERNAME, e.getMessage().equals("用户名或密码错误") ? "密码错误" : e.getMessage())));


    }
}
